package com.lanou.dormitory.web;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanou.dormitory.entity.Dormitory;
import com.lanou.dormitory.service.DormitoryService;
import com.lanou.dormitory.util.ResultUtil;

@Controller
public class DormitoryController {
	@Autowired
	private DormitoryService dormitoryService;
	
private String singlePath = "";//单图片的路径
	
	private StringBuffer sf = new StringBuffer();//多图片的路径
	
	@RequestMapping("/dormitory/insDormitory")
	@ResponseBody
	public ResultUtil save(String dormitoryNumber){
		
		try {
			//把 dormitoryNumber  singlePath  sf
			dormitoryService.save(dormitoryNumber, singlePath, sf.toString());
			return ResultUtil.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("新增失败");
		}
		
		
	}
	
	@RequestMapping("/dormitory/MultipleUpload")
	@ResponseBody
	public ResultUtil uploadMany(@RequestParam("file") List<MultipartFile> file,
			HttpServletRequest req) throws Exception{
		//图片上传的位置
		String path = req.getServletContext().getRealPath("/file");
		
		for(MultipartFile f : file){
			UUID uid = UUID.randomUUID();//给图片起一个新名称
			String filename = f.getOriginalFilename();
			//获取文件的后缀
			String suffix = filename.split("\\.")[1];
			//新的文件名称
			String newname = uid+"."+suffix;
			
			String filepath = path+File.separator+newname;//最终图片放的位置
			File fl = new File(filepath);
			
			f.transferTo(fl);//把图片的内容给传递到filepath中
			
			String pt = req.getContextPath()+"/file/"+newname;//一个图片的路径
			
			sf.append(pt+"~");
		}
		
		return ResultUtil.ok();
	}
	
	@RequestMapping("/dormitory/singleUpload")
	@ResponseBody
	public Map<String,String> uploadOne(MultipartFile file,
			HttpServletRequest req) throws Exception{
		String path = req.getServletContext().getRealPath("/file");	
		//给图片重新起一个名称
		UUID uid = UUID.randomUUID();
		String filename = file.getOriginalFilename();
		//获取文件的后缀
		String suffix = filename.split("\\.")[1];
		String newname = uid+"."+suffix;
		String filepath = path+File.separator+newname;//最终图片放的位置
		
		File f = new File(filepath);
		if(!f.exists()){
			f.createNewFile();
		}
		
		file.transferTo(f);//把图片的内容给传递到filepath中
		
		Map<String,String> map = new HashMap<>();
		map.put("code", "0");
		// /dt/file/fajljflajf.jpg
		map.put("image", req.getContextPath()+"/file/"+newname);
		
		return map;
	}
	
	@RequestMapping("/dormitory/addDormitory")
	public String toAdd(){
		return "/jsp/dormitory/addDormitroy";
	}
	
	@RequestMapping("/dormitory/dormitoryList")
	public String toList(){
		return "/jsp/dormitory/dormitoryList";
	}
	
	@RequestMapping("/dormitory/getDormitoryList")
	@ResponseBody
	public ResultUtil getList(Integer page,Integer limit){
		
		PageHelper.startPage(page, limit);
		
		List<Dormitory> list = dormitoryService.getAll();
		
		//处理list的分页信息
		PageInfo<Dormitory> info = new PageInfo<>(list);
		
		Long total = info.getTotal();//总条数
		List<Dormitory> pagelist = info.getList();//只显示10条数据
		
		ResultUtil result = new ResultUtil();//处理返回结果的
		result.setCode(0);
		result.setMsg("");
		result.setCount(total);
		result.setData(pagelist);
		
		return result;
	}
}
